package o2o.task.mixComTel;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.client.tel.MixComTelClient;
import o2o.cache.order.OrderInfoMemory;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.order.OrderInfo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//小号线程
@Component
public class MixComTelTask {
    //需要绑定的
    public static Map<String, String> telMap = new ConcurrentHashMap<String, String>();
    //需要解绑的
    public static Map<String, String> unbindMap = new ConcurrentHashMap<String, String>();

    @Scheduled(cron = "0/10 * * * * *")   //每10秒执行一次
    public void bindTel() {
        if (telMap.size() > 0) {
            synchronized (telMap) {
                //绑定小号
                for (String orderNo : telMap.keySet()) {
                    OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
                    if (null == orderInfo) {
                        continue;
                    }
                    if (null == orderInfo.getOrderType()) {
                        continue;
                    }
                    if (ValidateUtil.isNull(orderInfo.getPostmanPhone()) || ValidateUtil.isNull(orderInfo.getTelephone())) {
                        continue;
                    }
                    String phone = "";
                    try {
                        phone = MixComTelClient.getInsance("o2o-laoke", orderInfo.getPostmanPhone(), orderInfo.getTelephone()).bindTel();

                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    //存入数据库
                    StringBuffer sb = new StringBuffer();
                    sb.append("update ").append(PojoTables.OrderInfo).append(" set extra3 = '").append(phone).append("' where delFlag = 0 and orderNo = '").append(orderNo).append("'");
                    try {
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        continue;
                    }

                    //更新缓存
                    OrderInfoMemory.getInstance().modifyStatus(orderNo);

                    telMap.remove(orderNo);
                }
            }
        }

        if (unbindMap.size() > 0) {
            synchronized (unbindMap) {
                //绑定小号
                for (String orderNo : unbindMap.keySet()) {
                    OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
                    if (null == orderInfo) {
                        continue;
                    }
                    if (null == orderInfo.getOrderType()) {
                        continue;
                    }
                    if (ValidateUtil.isNull(orderInfo.getPostmanPhone()) || ValidateUtil.isNull(orderInfo.getTelephone())) {
                        continue;
                    }
                    try {
                        MixComTelClient.getInsance("o2o-laoke", orderInfo.getPostmanPhone(), orderInfo.getTelephone()).unbindTel();

                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }

                    unbindMap.remove(orderNo);
                }
            }
        }
    }
}
